import { Component, OnInit, Output, Input, EventEmitter, ChangeDetectorRef, OnChanges } from '@angular/core';
import { appService } from '../../../../../my-service/app.service';
import { UtilService } from '../../../../../my-service/util.service';
import { Router } from '@angular/router';
@Component({
  selector: 'Timeline-root',
  templateUrl: './Timeline.component.html',
  styleUrls: ['./Timeline.component.css']
})
export class TimelineComponent implements OnInit, OnChanges {
  @Input() train: any;
  @Input() icon: any;

  @Output() ondel = new EventEmitter<Event>();
  @Output() onedit = new EventEmitter<Event>();
  colorfont = ['#FF0000', '#FF7F00', '#d23e99', '#00FFFF', '#0000FF', '#009999', '#FF3366', '#999933', '#7FFF00', '#7F00FF']
  timelist = []
  yxz = ['3', '3', '7', '7', '9']
  edit = false;
  month;
  height = 155;
  addwindow = false;
  img;
  constructor(public ref: ChangeDetectorRef, private appservice: appService, private router: Router, private utilService: UtilService) {
    this.img=this.utilService.canvas().img
   }
  ngOnChanges() {
    this.update()
  }
  ngOnInit() {
    this.update()
  }
  update() {
    this.month = this.getmonth()
    this.timelist = [
      { year: this.getyear(), month: this.month, data: [], height: 0 },
      { year: this.getyear(), month: (this.month - 1) > 0 ? (this.month - 1) : 12, data: [], height: 0 },
    ]
    for (let i = 0; i < this.train.length; i++) {
      // this.train[i].PasswordArray = this.train[i].Password.split('')
      if (this.train[i].CreateTime.split("-")[0] == this.getyear() && this.train[i].CreateTime.split("-")[1] == this.timelist[0].month) {
        this.addtime(0, i)
      }
      else if (this.train[i].CreateTime.split("-")[1] == this.timelist[1].month) {
        this.addtime(1, i)
      }
    }
  }

  addtimelist() {
    let num = 0
    for (let k = 0; k < this.timelist.length; k++) {
      num += this.timelist[k].data.length
    }
    if (num == this.train.length) {
      this.utilService.remind('error', '没有更早的训练了！')
      return false
    }
    else {
      this.timelist.push({
        month: (this.month - this.timelist.length) > 0 ? (this.month - this.timelist.length) : (12 - (this.timelist.length - this.month)),
        data: [],
        height: 0
      })
      for (let i = 0; i < this.train.length; i++) {
        if (this.train[i].CreateTime.split("-")[1] == this.timelist[this.timelist.length - 1].month) {
          this.addtime(this.timelist.length - 1, i)
        }
      }
    }
  }
  addtime(k, i) {
    this.timelist[k].data.push(this.train[i])
    this.timelist[k].height = Math.ceil(this.timelist[k].data.length / 4) * this.height
  }
  getmonth() {
    let date = new Date();
    let month = date.getMonth() + 1;
    return month
  }
  getyear() {
    let date = new Date();
    let year = date.getFullYear()
    return year
  }

  del(item, j, i) {
    this.appservice.deltrain({
      PK_TrainningGuid: item.PK_TrainningGuid,
      Status: 0
    }).then(re => {
      if (re.status == 1) {
        this.timelist[j].data.splice(i, 1);
        this.ondel.emit()
        this.utilService.remind('success', '删除成功！')
      }
    })
  }
  goto(path) {
    this.router.navigate([`${path}`]);
  }
  edititem(item) {
    this.onedit.emit(item)
  }
}
